package com.ming.service.impl;
import com.ming.dao.ProductCategoryRepository;
import com.ming.dto.OrderDTO;
import com.ming.pojo.ProductCategory;
import com.ming.service.BuyerService;
import com.ming.service.CategoryService;
import com.ming.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by LIANG on 2018/3/22.
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private OrderService orderService;

    @Override
    public OrderDTO findOrderOne(String openid, String orderId) {
        return checkOrderOwner(openid,orderId);
    }

    @Override
    public OrderDTO cancelOrder(String openid, String orderId) {
        OrderDTO orderDTO = checkOrderOwner(openid,orderId);
        if (orderDTO == null){
            log.error("【取消订单】查不到订单，orderDTO={}",orderDTO);
        }
        return orderService.cancel(orderDTO);
    }

    public OrderDTO checkOrderOwner(String openid,String orderId){
        OrderDTO orderDTO =orderService.findOne(orderId);
        if (orderDTO ==null){
            return null;
        }
        if (!orderDTO.getBuyerOpenid().equals(openid)){
            log.error("【查询订单】订单的ID不一致，openid={},orderDTO={}",openid,openid);
        }
        return orderDTO;
    }

    /**
     * 类目
     * Created by 廖师兄
     * 2017-05-09 10:16
     */
    @Service
    public static class CategoryServiceImpl implements CategoryService {

        @Autowired
        private ProductCategoryRepository repository;

        @Override
        public ProductCategory findOne(Integer categoryId) {
            return repository.findOne(categoryId);
        }

        @Override
        public List<ProductCategory> findAll() {
            return repository.findAll();
        }

        @Override
        public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
            return repository.findByCategoryTypeIn(categoryTypeList);
        }

        @Override
        public ProductCategory save(ProductCategory productCategory) {
            return repository.save(productCategory);
        }
    }
}
